package com.jingyanzi.resallocation.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.resallocation.dao.ResSampleMapper;

import com.jingyanzi.resallocation.domain.SampleDO;
import com.jingyanzi.resallocation.entity.ResSample;
import com.jingyanzi.resallocation.entity.ResSampleExample;
import com.jingyanzi.resallocation.entity.ResSampleExample.Criteria;
import com.jingyanzi.resallocation.query.SampleQueryDO;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.service.ISampleService;
import com.jingyanzi.resallocation.util.BeanUtilsExtends;

@Service("sampleService")
public class SampleServiceImpl extends BaseServiceImpl implements ISampleService {
	@Autowired
	private ResSampleMapper sampleMapper;

	public ResultDO add(SampleDO sampleDO) {
		ResSample sample = new ResSample();

		ResultSupport result = BeanUtilsExtends.copy(sample, sampleDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		sample.setCreatetime(Calendar.getInstance().getTime());
		sample.setIsDelete(1);
		int r = -1;

		try {
			r = sampleMapper.insert(sample);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加看样中心失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加看样中心失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO edit(SampleDO sampleDO) {
		ResSample sample = new ResSample();

		ResultSupport result = BeanUtilsExtends.copy(sample, sampleDO);
		if (!result.isSuccess())
			return result;
		int r = -1;

		try {
			r = sampleMapper.updateByPrimaryKeySelective(sample);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改看样中心失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改看样中心失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO del(Integer[] id) {
		ResSample sample = new ResSample();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < id.length; i++) {
			sample.setId(id[i]);
			sample.setIsDelete(-1);
			int r = -1;

			try {
				r = sampleMapper.updateByPrimaryKeySelective(sample);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除看样中心失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除看样中心失败");
				result.setSuccess(false);
				return result;
			}
		}

		return result;
	}

	public ResultDO get(Integer id) {
		ResSample sample = new ResSample();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		try {
			sample = sampleMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取看样中心失败");
			result.setSuccess(false);
			return result;
		}

		if (sample == null) {
			result.setErrorCode(ResultDO.SALESGROUP_GET_ERROR);
			result.setErrorMsg("不存在该看样中心");
			result.setSuccess(false);
			return result;
		}
		SampleDO sampleDO = getSampleDO(sample);
		result.setModel(ResultDO.FIRST_MODEL_KEY, sampleDO);
		return result;
	}

	public ResultDO query(SampleQueryDO sampleQueryDO) {
		ResultSupport result = new ResultSupport();

		List<ResSample> list = null;

		ResSampleExample example = new ResSampleExample();

		Criteria createCriteria = example.createCriteria();
		if (sampleQueryDO.getType() != null && sampleQueryDO.getType() != 0) {
			createCriteria.andTypeEqualTo(sampleQueryDO.getType());
		}
		createCriteria.andIsDeleteEqualTo(1);
		List<SampleDO> sampleDOs = null;

		if (sampleQueryDO.isPage()) {
			example.setLimitByPage(sampleQueryDO.getStart() + "," + sampleQueryDO.getPageRows());
			long count = sampleMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = sampleMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取看样中心失败");
			result.setSuccess(false);
			return result;
		}

		sampleDOs = getSampleDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, sampleDOs);
		return result;
	}

}
